﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using log4net;

namespace XRunner.SDK.XUtill
{
    public class HTTPUtill
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(HTTPUtill));
        #region HTTPHEADER
        private const string contentType = "application/x-www-form-urlencoded";
        private const string accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-silverlight-2-b1, */*";
        private const string userAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
        private static Encoding encoding = Encoding.UTF8;
        #endregion
        /// <summary>
        /// 私有化构造方法
        /// </summary>
        private HTTPUtill()
        {
        }
        public static string doSend(string url, string postData, string type, CookieContainer cc)
        {
            if (log.IsDebugEnabled)
            {
                log.Debug(string.Format("url:{0};postData:{1};type:{2}", url, postData, type));
            }
            if (string.IsNullOrEmpty(postData))
            {
                type = "GET";
            }
            try
            {
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
                request.CookieContainer = cc;
                request.ContentType = contentType;
                request.Referer = url;
                request.Accept = accept;
                request.UserAgent = userAgent;
                request.Method = type;
                if ("POST".Equals(type, StringComparison.InvariantCultureIgnoreCase))
                {
                    // 只有POST方法需要向流中写入信息
                    byte[] reqData = encoding.GetBytes(postData);
                    request.ContentLength = reqData.Length;
                    Stream stream = request.GetRequestStream();
                    stream.Write(reqData, 0, reqData.Length);
                    stream.Close();
                }
                HttpWebResponse response;
                response = (HttpWebResponse)request.GetResponse();
                Stream responseStream = response.GetResponseStream();
                StreamReader streamReader = new StreamReader(responseStream, encoding);
                string html = streamReader.ReadToEnd();
                streamReader.Close();
                responseStream.Close();
                if (log.IsDebugEnabled)
                {
                    log.Debug(string.Format("html:{0}",html));
                }
                return html;
            }
            catch (Exception e)
            {
                log.Error(string.Format("ErrType:{0}; ErrMsg:{1}", e.GetType(), e.Message));
            }
            return string.Empty;
        }
    }
}
